<?php
/**
* admin
*/
class adminController
{
	public $auth;

	public function __construct()
	{
		$authobj = M('auth');
		$this->auth = $authobj->getauth();
		VIEW::assign(array('authinfo' => $this->auth));
		if (empty($this->auth) && (PC::$method != 'login')) {
			$this->showmessage('请登录后再操作！', 'admin.php?controller=admin&method=login');
		}
	}

	public function index()
	{
		$newsobj = M('news');
		$newsnum = $newsobj->count();
		VIEW::assign(array('newsnum' => $newsnum));
		VIEW::display('admin/index.html');
	}

	public function login()
	{
		if ($_POST) {
			$this->checklogin();
		} else {
			VIEW::display('admin/login.html');
		}
	}

	public function logout()
	{
		$authobj = M('auth');
		$authobj->logout();
		$this->showmessage('退出成功', 'admin.php?controller=admin&method=login');
	}

	private function checklogin()
	{
		$authobj = M('auth');
		if ($authobj->loginsubmit()) {
			$this->showmessage('登录成功', 'admin.php?controller=admin&method=index');
		} else {
			$this->showmessage('登录失败', 'admin.php?controller=admin&method=login');
		}
	}

	public function newsadd()
	{
		if (empty($_POST)) {
			if (isset($_GET['id'])) {
				$data = M('news')->getnewsinfo($_GET['id']);
			} else {
				$data = array();
			}
			VIEW::assign(array('data' => $data));
			VIEW::display('admin/newsadd.html');
		} else {
			$this->newssubmit();
		}
	}

	private function newssubmit()
	{
		$news = M('news');
		$res = $news->newssubmit($_POST);
		switch ($res) {
			case 0:
				$this->showmessage('操作失败', 'admin.php?controller=admin&method=newsadd&id=' . $_POST['id']);
				break;
			case 1:
				$this->showmessage('添加成功', 'admin.php?controller=admin&method=newslist');
				break;
			case 2:
				$this->showmessage('修改成功', 'admin.php?controller=admin&method=newslist');
				break;
			default:
				break;
		}
	}

	public function newslist()
	{
		$newsobj = M('news');
		$data = $newsobj->findAll_orderby_dateline();
		VIEW::assign(array('data' => $data));
		VIEW::display('admin/newslist.html');
	}

	public function newsdel()
	{
		if ($_GET['id']) {
			$newsobj = M('news');
			$newsobj->delnews($_GET['id']);
			$this->showmessage('删除新闻成功！', 'admin.php?controller=admin&method=newslist');
		}
	}

	private function showmessage($info, $url)
	{
		exit("<script>alert('$info');window.location.href='$url'</script>");
	}
}